package net.bolue.console.data.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.bolue.console.data.InsertInfo;
import net.bolue.console.model.BusiBase;
import net.bolue.sysmng.exception.BolueException;
import net.bolue.sysmng.model.ColInfoModel;
import net.bolue.sysmng.model.InsResModel;
import net.bolue.sysmng.model.ReportResModel;
import net.bolue.sysmng.tblbean.ColInfo;
import net.bolue.sysmng.tblbean.DbopResMap;
import net.bolue.sysmng.tblbean.InsRes;
import net.bolue.sysmng.util.DictUtil;
import net.bolue.web.entry.FormDataOption;
import net.bolue.web.entry.FormElemTag;
import net.bolue.web.report.RepConstants;
import net.bolue.web.util.RequestUtil;
import net.bolue.web.util.TagUtil;

/**
 * 新增信息业务
 * @author Administrator
 *
 */
public class InsertInfoModel extends BusiBase{
	
	private InsertInfo insertInfo;
	private InsResModel insResModel;
	private ColInfoModel colInfoModel;

	public InsertInfoModel() {
		super();
	}

	public InsertInfoModel(HttpServletRequest request) throws BolueException{
		super(request);
		this.insertInfo = new InsertInfo();
		this.init();
	}
	
	public InsertInfoModel(HttpServletRequest request,InsRes insRes) throws BolueException{
		super(request);
		this.insertInfo = new InsertInfo();
		this.insertInfo.setInsRes(insRes);
		this.init();
	}
	
	public void init() throws BolueException{
		this.initParam();
		insResModel = new InsResModel(request);
		colInfoModel = new ColInfoModel(request);
	}

	/**
	 * 获得新增信息
	 * @return
	 * @throws BolueException
	 */
	public InsertInfo getInsertInfo() throws BolueException{
		String subSys = this.insertInfo.getSubSys();
		InsRes insRes = this.insertInfo.getInsRes();
		if(insRes==null){
			insRes = insResModel.getInsRes(subSys, this.insertInfo.getInsertId());
			this.insertInfo.setInsRes(insRes);
		}
		String tableName = insRes.getTableName();
		List<ColInfo> colInfoList = this.insertInfo.getColInfoList();
		if(colInfoList==null){
			colInfoList = colInfoModel.getColInfoList(subSys, tableName);
			this.insertInfo.setColInfoList(colInfoList);
		}
		parseColList();
		return insertInfo;
	}
	
	/**
	 * 初始化新增参数
	 * @throws BolueException
	 */
	public void initParam() throws BolueException{
		if(this.insertInfo==null){
			return;
		}
		InsRes insRes = this.insertInfo.getInsRes();
		if(insRes!=null){
			this.insertInfo.setInsertId(insRes.getInsertId());
			this.insertInfo.setSubSys(insRes.getSubSys());
			return;
		}
		String subSys = RequestUtil.getParamValue(request, RepConstants.REP_SUBSYS_KEY);
		if (subSys == null){
			throw new BolueException(1, "必须提供request参数subSys!");
		}
		String insertID = RequestUtil.getParamValue(request, RepConstants.REP_INSERTID_KEY);
		if (insertID == null){
			String repId = RequestUtil.getParamValue(request, RepConstants.REP_REPID_KEY);
			if (repId == null){
				throw new BolueException(1, "必须提供request参数insertID!");
			}
			int i = new Integer(repId).intValue();
			ReportResModel reportResModel = new ReportResModel();
			DbopResMap dbopResMap = reportResModel.getDBOpResMap(subSys, i, 2);
			if (dbopResMap == null){
				throw new BolueException(1, "没有找到报表" + subSys + "." + i + "的新增资源映谢!");
			}
			insertID = dbopResMap.getResId();
		}
		this.insertInfo.setInsertId(insertID);
		this.insertInfo.setSubSys(subSys);
	}
	
	/**
	 * 处理表列信息
	 * @throws BolueException
	 */
	public void parseColList() throws BolueException{
		List<ColInfo> colInfoList = this.insertInfo.getColInfoList();
		if(colInfoList==null||colInfoList.size()<1){
			return;
		}
		List<FormElemTag> elemTagList = new ArrayList<FormElemTag>();
		String[] subSysArray = new String[colInfoList.size()];
		Integer[] dicKindArray = new Integer[colInfoList.size()];
		Integer[] dicGrpIdArray = new Integer[colInfoList.size()];
		int k = 0;
		InsRes insRes = this.insertInfo.getInsRes();
		if(insRes==null){
			return;
		}
		String insCols = insRes.getInsCols();
		if(insCols!=null&&insCols.length()>0){
			insCols = ","+insCols+",";
		}else{
			insCols = "";
		}
		String dispCols = insRes.getDispCols();
		if(dispCols!=null&&dispCols.length()>0){
			dispCols = ","+dispCols+",";
		}else{
			dispCols = "";
		}
		for(ColInfo colInfo : colInfoList){
			String colName = colInfo.getColName();
			if(!insCols.matches(".*,"+colName+",.*")){
				continue;
			}
			FormElemTag formElemTag = TagUtil.colToField(colInfo);
			Object initValue = request.getParameter("col_"+colName);
			formElemTag.setInitValue(initValue);
			if(colInfo.getTagType()!=4){
				if(dispCols.matches(".*,"+colName+",.*")){
					formElemTag.setTagType(-1);
				}
			}
			elemTagList.add(formElemTag);
			subSysArray[k] = colInfo.getSubSys();
			dicKindArray[k] = colInfo.getDicKind();
			dicGrpIdArray[k] = colInfo.getDicGrpid();
			k++;
		}
		Map<String,List<FormDataOption>> dicsMap = DictUtil.getDicMaps(subSysArray, dicKindArray, dicGrpIdArray);
		this.insertInfo.setDicsMap(dicsMap);
		this.insertInfo.setElemTagList(elemTagList);
	}
}
